package cn.edu.bit.lixin.blog.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDAO extends HibernateDaoSupport {
	
	/**
	 * save an entity
	 * @param entity
	 */
	public void save(Object entity){
		this.getHibernateTemplate().save(entity);
	}
	
	/**
	 * saveOrUpdate an entity
	 * @param entity
	 */
	public void saveOrUpdate(Object entity){
		this.getHibernateTemplate().saveOrUpdate(entity);
	}
	
	/**
	 * find entities by hql
	 * @param hql
	 * @param values
	 * @return list of entities
	 */
	public List findByHQL(String hql, Object... values){
		System.out.println(hql);
		return this.getHibernateTemplate().find(hql, values);
	}
	
	/**
	 * find entities by property
	 * @param entityName
	 * @param propertyName
	 * @param value
	 * @return list of entities
	 */
	public List findByProperty(String entityName, String propertyName, Object value){
		String hql = "from " + entityName + " where " + propertyName + "=?";
		return this.findByHQL(hql, value);
	}
	
	
	/**
	 * find an entity by id and type
	 * @param type
	 * @param id
	 * @return Object
	 */
	public Object findById(Class type,java.io.Serializable id){
		return this.getHibernateTemplate().get(type, id);
	}
	
	/**
	 * find an entity by id and entityName
	 * @param entityName
	 * @param id
	 * @return Object
	 */
	public Object findById(String entityName,java.io.Serializable id){
		return this.getHibernateTemplate().get(entityName, id);
	}
	
	/**
	 * delete an entity
	 * @param entity
	 */
	public void delete(Object entity){
		this.getHibernateTemplate().delete(entity);
	}
	
	/**
	 * delete an entity by id
	 * @param entityName
	 * @param id
	 * @return Integer
	 */
	public Integer deleteById(String entityName,final java.io.Serializable id){
		final String hql = "delete" + entityName + "where id=" + id;
		return (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					public Object doInHibernate(Session session) throws HibernateException,SQLException{
						Query query = session.createQuery(hql);
						return query.executeUpdate();
					}
				}
		);
	}
	
	/**
	 * update an entity
	 * @param entity
	 */
	public void update(Object entity){
		this.getHibernateTemplate().update(entity);
	}
	
	/**
	 * find all entities from entityName
	 * @param entityName
	 * @return list of entities
	 */
	public List findAll(String entityName){
		//return this.findByHQL("from" + entityName);
		List list = null;
		try{
			 list = this.findByHQL("from " + entityName);
		}catch (Exception e){
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * find entity by name
	 * @param entityName
	 * @param header header is name of header,the header of username is 'user'
	 * @param name
	 * @return list of entities
	 */
	public List findByName(String entityName, String header, String name){
		return findByProperty(entityName, header+"name", name);
	}
	
	/**
	 * query the results of some page
	 * @param hql 
	 * @param currentPage
	 * @param pageSize
	 * @param values
	 * @return all the results of the page
	 */
	public List pagedQuery(final String hql, int currentPage, final int pageSize, final Object... values){
		if (currentPage == 0){
			currentPage = 1;
		}
		
		final int curPage = currentPage;
		
		List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException{
				Query query = session.createQuery(hql);
				if (values != null){
					for (int i = 0;i < values.length;i++){
						query.setParameter(i, values[i]);
					}
				}
				
				List result = query.setFirstResult((curPage - 1) * pageSize)
				.setMaxResults(pageSize).list();
				return result;
			}
		});
		return list;
	}
	
	
	/**
	 * 将查询COUNT结果作为长整数来显示. HQL = select count(id) from Entity
	 * 
	 * @param hql 查询语句
	 * @param args 参数列表
	 * @return 整型数字
	 */
	public Integer queryForCount( final String hql, final Object... args) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			 public Object doInHibernate(Session session)
			 throws HibernateException, SQLException {
			 Query query = session.createQuery(hql);
			 
			 for(int i =0; i < args.length; i++) {
				 query.setParameter(i, args[i]);
			 }
			 

			 List list = query.list();
			 return list;
			 }
			});
		
		// Hibernate做count计算返回一般都是对象
		
		return Integer.parseInt(list.get(0) + "");		
	}
}








